var filter = {LOCATION:{x:0,y:0},
                   STYPE:'Brothel',
                   NATION:'Caucasion',
                   RADIUS:1,
                   PRICE:{MIN:40,MAX:90}
                  }

function document_load(){
  zoyoe.map = new MapInfoCore(function(){},null,null,null);
  SetHandler('service-type',function(data){
      filter.STYPE = data;
    }
  );
  SetHandler('nationality',function(data){
      filter.NATION = data;
    }
  );
  SetHandler('radius',function(data){
      filter.RADIUS = parseInt(data);
    }
  );
  SetHandler('max',function(data){
      filter.PRICE.MAX = data;
    }
  );
  SetHandler('min',function(data){
      filter.PRICE.MIN = data;
    }
  );
  YUI().use('node',function(Y){
    Y.one('#search-btn').on('click',function(e){
      PreSearch();
    });
  });
}
function SetHandler(listname,update_function){
  YUI().use('node',function(Y){
    var service_type_current = Y.one('#' + listname+" .list .current")
    var items = Y.all('#' + listname+" .list ul li");
    items.on('click', function(e){
      var spans = Y.all('#' + listname + " .list ul li span");
      spans.set('innerHTML',"&#9744");
      var raw_node = Y.Node.getDOMNode(e.currentTarget);
      var data = raw_node.firstChild.data;
      service_type_current.set('innerHTML',data);
      raw_node.innerHTML = data + "<span>&#9746</span>";
      update_function(data);
    });
  });
}
function Search(result){
  if(result && result.resourceSets){
    if(result.resourceSets[0].estimatedTotal = 0){
      zoyoe.ALERT('Address Not Found !!');
    }
    else if(result.resourceSets[0].estimatedTotal = 1){
      var resources = result.resourceSets[0].resources;
      if(resources.length == 0){
        zoyoe.ALERT('Address Not Found !!');
      }else if(resources.length == 1){
        zoyoe.ALERT(resources[0].address.formattedAddress);
        var c = resources[0].point.coordinates;
        zoyoe.ALERT(c);
        filter.LOCATION.x = c[0];
        filter.LOCATION.y = c[1];
      }else{
        zoyoe.ALERT("Ambiguious Address, Please Make It Clear !!");
      }
    }
    else{
      zoyoe.ALERT('Multi Address Found,Please Make It Clear !!');
    }
  }
  YUI().use("json-stringify","node",function (Y) {
    var filterStr = Y.JSON.stringify(filter);
    var searchStr = Y.Node.getDOMNode(Y.one("#search-bar input")).value;
    window.location.href = "/adult/core/services/?"+"filter=" + filterStr;
  });
}

function PreSearch(){
  YUI().use("json-stringify","node",function (Y) {
    var searchStr = Y.Node.getDOMNode(Y.one("#search-bar input")).value;
    if(searchStr == ""){
      var filterStr = Y.JSON.stringify(filter);
      filter.LOCATION.x = 0;
      filter.LOCATION.y = 0;
      window.location.href = "/adult/core/services/?"+"filter=" + filterStr;
    }
    zoyoe.ALERT(searchStr);
    zoyoe.map.TargetPlace(searchStr,"Search")
  });
}
